package com.hugoo.imp.slt;

import com.hugoo.core.db.DBC;
import com.hugoo.core.db.HSF;
import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import com.hugoo.core.util.TL;
import com.hugoo.imp.buzz.ImpInfo;
import com.hugoo.imp.buzz.TxtImpBuzz;
import com.hugoo.imp.buzz.XlsImpBuzz;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public class ImpThread extends Thread {

    static boolean flag = true;
    private static final Log log = LogFactory.getLog(ImpThread.class);

    public static void stopImp() {
        flag = false;
    }

    @Override
    public synchronized void start() {
        super.start();
        this.setName("FILE-IMP-THEADER");
    }

    @Override
    public void run() {
        ImpQueue iq = ImpQueue.ref();
        while (flag) {
            String code = iq.getCode();
            if (code != null) {
                log.info("存在需要数据的数据记录，启动数据导入程序！");
                //
                try {
                    RecordSet<Record> rs = HSF.query(null, "select * from T_SYS_IMP_FILEMETA where substr(ISPASS,3,1)='W' "); //需要转换成Continal;
                    while (rs.next()) {
                        String ext = rs.getString("FILEXT");
                        if (TL.isEmpty(ext)) {
                            continue;
                        }
                        ext = ext.toLowerCase();
                        ImpInfo ib = null;
                        String scod = rs.getString("SCOD");
                        String extpara = rs.getClobStr("EXTPARA");
                        log.info("参数值::" + extpara);
                        Map m = new HashMap();
                        if (TL.isNotEmpty(extpara)) {
                            TL.parsePara(extpara, m, log);
                        }
                        log.info("自动执行导入程序参数值：" + m);
                        if (ext.endsWith("txt")) {
                            ib = new TxtImpBuzz(rs.getString("SID"), rs.getString("FILEPATH"), scod, m);//94828
                        } else if (ext.endsWith("xls")) {
                            ib = new XlsImpBuzz(rs.getString("SID"), rs.getString("FILEPATH"), scod, m);//94828
                        }
                        if (ib != null) {
                            DBC dbc = new DBC();
                            dbc.initDatabase(ib);
                            boolean f = dbc.impData(ib);
                            String ips = rs.getString("ISPASS");
                            if ("YXW-".equalsIgnoreCase(ips)) {//直接导入的流程
                                ips = f ? "YXYY" : "YXNN";//【基本YNWX】【审核YNWX-】【导入YNWX-】【是否成功YN-】
                            } else if ("Y-W-".equalsIgnoreCase(ips)) {//先导后审的流程
                                ips = f ? "YWY-" : "YXNN";
                            } else if ("YYW-".equalsIgnoreCase(ips)) {//审核完成后的流程
                                ips = f ? "YYYY" : "YXNN";
                            }
                            HSF.excute(null, "update T_SYS_IMP_FILEMETA set ISPASS='" + ips + "' where SCOD='" + scod + "'");
                            String imd = rs.getString("IMP_METHOD");
                            //后期文件处理
                            ib.lstProcFile(imd);
                        } else {
                            log.info("未正常初始化，无效的类型");
                        }
                    }

                } catch (Exception ex) {
                    log.warn("执行导入程序，数据异常！", ex);
                }
            }
            try {
                Thread.yield();
                sleep(1000L);
            } catch (Exception e) {
                log.warn("线程异常：：", e);
            }
        }
        log.info("正常关闭线程！");
    }
}
